/**
 * Nombre : PermisoPerfilAction.java 
 * Versi�n : 1.1 
 * Creaci�n : 14/11/2008 
 * Autor : Orlando Pihue Montoya 
 * Usuario : P020912 
 * Finalidad : Consulta de Perfiles de Usuario
 * Modificacion: << fecha de modificaci�n>> 
 * Autor : <<quien actualizo el archivo>> 
 * Usuario : <<c�digo de usuario modificador>> 
 * Detalle : <<descripci�n de la modificaci�n>>
 */
 
package igrupobbva.pe.sistemareniec.actions;

import igrupobbva.pe.sistemareniec.forms.CorporativaForm;

import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.grupobbva.iici.serializable.Corporativa;
import com.grupobbva.iici.service.CorporativaService;
import com.grupobbva.iici.util.SpringUtils;


public class EstadoAction extends DispatchAction

{
	static Logger logger = Logger.getLogger(EstadoAction.class.getName());
	
	CorporativaService corporativaService;
	 
	public ActionForward inicio(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		corporativaService=SpringUtils.getBean(request, CorporativaService.class);
		String flag1=(String)sesion.getAttribute("root");
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
	
		ActionForward forward = new ActionForward(); 
		CorporativaForm objForm = (CorporativaForm) form;
		try {			
			List listaEstado = corporativaService.obtenerCatalogoMultitabla("T03");
			request.getSession().setAttribute("listaEstado", listaEstado);
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error");
		} else {
			forward = mapping.findForward("ok");
		}
		return (forward);
	}
	
	public ActionForward mostrarNuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward(); // return value
		
		CorporativaForm corporativaForm = (CorporativaForm) form;
					
		try { 
			
			corporativaForm.setCodEle("");
			corporativaForm.setValor1("");
			corporativaForm.setValor2("");
			corporativaForm.setValor3("");
			corporativaForm.setValor4("");
			request.setAttribute("method", "nuevo");
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));  //$NON-NLS-2$
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		return (forward);
	}
	
	public ActionForward nuevo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		ActionForward forward = new ActionForward();
		CorporativaForm corporativaForm = (CorporativaForm) form;
		corporativaService=SpringUtils.getBean(request, CorporativaService.class);
		
		String codMult = "T03";
		String codEle = (String)request.getParameter("codEle");
		String valor1 = (String)request.getParameter("valor1");
		String valor2 = (String)request.getParameter("valor2");

		int flag =Integer.parseInt((String) sesion.getAttribute("root")) ;
		
		Corporativa corporativa = new Corporativa();
		
		corporativa.setCodMult("T03");
		corporativa.setCodEle(codEle);
		corporativa.setValor1(valor1);
		corporativa.setValor2(valor2);
		
		try { 			
			corporativaService.registrar(corporativa);
			request.getSession().setAttribute("listaEstado", corporativaService.obtenerCatalogoMultitabla("T03"));	
			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);
	}
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String) sesion.getAttribute("root")) ;
		
		ActionForward forward = new ActionForward();
		CorporativaForm estadoForm = (CorporativaForm) form;
		corporativaService=SpringUtils.getBean(request, CorporativaService.class);
		
		String codMult= (String) request.getParameter("codMult");
		String codEle = request.getParameter("codEle");
		
		Corporativa corporativa = new Corporativa();
		corporativa.setCodMult(codMult);
		corporativa.setCodEle(codEle);
		
		
		try {			

			corporativaService.eliminar(corporativa);
			return inicio(mapping, form, request, response);				
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		return (forward);
	}
	
	public ActionForward mostrarEditar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root")) ;
		ActionForward forward = new ActionForward(); 
		Vector listMenu = null;	
		
		String codEle = (String) request.getParameter("codEle");	
		String codMult =(String) request.getParameter("codMult");
		String valor1 = (String) request.getParameter("valor1");	
		String valor2 = (String) request.getParameter("valor2");
		String valor3 = (String) request.getParameter("valor3");
		String valor4 = (String) request.getParameter("valor4");
	
		
		CorporativaForm objCorportativa = (CorporativaForm) form;
		
		try { 

			Corporativa corporativa = new Corporativa();
			objCorportativa.setCodEle(codEle);
			objCorportativa.setCodMult(codMult);
			objCorportativa.setCodMult(valor1);
			objCorportativa.setCodMult(valor2);
			objCorportativa.setCodMult(valor3);
			objCorportativa.setCodMult(valor4);
			sesion.setAttribute("codigoElemento", codEle);
			sesion.setAttribute("corporativa", corporativa);
		    request.setAttribute("method", "editar");
						
		} catch (Exception e) {
			e.printStackTrace();
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("error"); 
		} else {
			forward = mapping.findForward("nuevo"); 
		}
		return (forward);	
	}	
	
	

	
	public ActionForward editar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		HttpSession sesion = request.getSession();
		ActionErrors errors = new ActionErrors();
		int flag =Integer.parseInt((String)sesion.getAttribute("root"));
		ActionForward forward = new ActionForward();	
		corporativaService=SpringUtils.getBean(request, CorporativaService.class);
		
		Vector listMenu = null;
		String codMult = "T03";
		String codEle = (String)request.getParameter("codEle");
		String valor1 = (String)request.getParameter("valor1");
		String valor2 = (String)request.getParameter("valor2");
		
		
		try { 		
			Corporativa corporativa = new Corporativa();
			corporativa.setCodMult(codMult);
			corporativa.setCodEle(codEle);
			corporativa.setValor1(valor1.trim());
			corporativa.setValor2(valor2.trim());
			corporativaService.modificar(corporativa);
			request.getSession().setAttribute("listaEstado", corporativaService.obtenerCatalogoMultitabla("T03"));			
			
		} catch (Exception e) {
			errors.add("name", new ActionError("id"));
		}
		if (!errors.isEmpty()) {
			saveErrors(request, errors);
			forward = mapping.findForward("ok"); 
		} else {
			forward = mapping.findForward("ok"); 
		}
		// Finish with
		return (forward);
	}
}
